FROM {{ conductor_base }}
ENV ANSIBLE_CONTAINER=1
{% set distro = conductor_base.split(':')[0] %}
{% set distro_version = conductor_base.split(':')[1] %}
{% for envar in environment %}
ENV {{ envar }}
{% endfor %}
{% if distro in ["fedora"] %}
RUN dnf update -y && \
    dnf install -y make gcc git python2 python2-devel curl rsync libffi-devel openssl-devel redhat-rpm-config python2-dnf tar redhat-rpm-config && \
    dnf clean all
{% elif distro in ["centos"] %}
RUN yum update -y && \
    yum install -y epel-release && \
    yum install -y make gcc git python-devel curl rsync libffi-devel openssl-devel && \
    yum clean all
{% elif distro in ["amazonlinux"] %}
RUN yum -y update && \
    yum -y install make git gcc python27-devel rsync libffi-devel openssl-devel tar && \
    yum clean all
{% elif "rhel" in distro %}
RUN yum -y update-minimal --disablerepo "*" \
                          --enablerepo rhel-7-server-rpms,rhel-7-server-optional-rpms \
                          --security --sec-severity=Important --sec-severity=Critical --setopt=tsflags=nodocs && \
    yum -y install --disablerepo "*" \
                   --enablerepo rhel-7-server-rpms \
                   https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && \
    yum -y install --disablerepo "*" \
                   --enablerepo epel,rhel-7-server-rpms,rhel-7-server-optional-rpms \
                   make gcc git python-devel curl rsync libffi-devel openssl-devel tar redhat-rpm-config && \
    yum clean all
{% elif distro in ["debian", "ubuntu"] %}
RUN apt-get update -y && \
    apt-get install -y make dpkg-dev curl gcc git libffi-dev libpopt0 libssl-dev python2.7 python-apt python-dev rsync sudo && \
    cd /usr/bin && \
    rm -f lsb_release && \
    ln -fs python2.7 python && \
    {% if distro_version in ["precise"] %}
    cd /usr && \
    ln -fs ../../include/python2.7 local/include/python2.7 && \
    {% endif %}
    apt-get clean
{% elif distro in ["alpine"] %}
# openssh is necessary until this is fixed: https://github.com/ansible/ansible/issues/24705
RUN apk add --no-cache -U python-dev make git curl rsync libffi libffi-dev openssl openssl-dev gcc musl-dev tar openssh
{% endif %}

RUN (curl https://bootstrap.pypa.io/get-pip.py | python - --no-cache-dir ) && \
    mkdir -p /etc/ansible/roles /_ansible/src && \
    mkdir -p /licenses && \
    {% if distro_version in ["wheezy", "precise", "trusty"] %}
    pip install --no-cache-dir -U pyopenssl ndg-httpsclient pyasn1 && \
    {% endif %}
    (curl https://get.docker.com/builds/Linux/x86_64/docker-{{ docker_version }}.tgz \
       | tar -zxC /usr/local/bin/ --strip-components=1 docker/docker )

ADD LICENSE /licenses/LICENSE
ADD help.1 /help.1

# The COPY here will break cache if the version of Ansible Container changed
COPY /container-src /_ansible/container

RUN cd /_ansible && \
    pip install --no-cache-dir -r container/conductor-build/conductor-requirements.txt && \
    PYTHONPATH=. LC_ALL="en_US.UTF-8" python container/conductor-build/setup.py develop -v -N && \
    ansible-galaxy install -p /etc/ansible/roles -r container/conductor-build/conductor-requirements.yml

